package io.renren.modules.yckj.controller;

import io.renren.common.annotation.LogOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;

import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.yckj.dto.AddressDTO;
import io.renren.modules.yckj.excel.AddressExcel;
import io.renren.modules.yckj.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 发票管理
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2025-04-16
 */
@RestController
@RequestMapping("yckj/address")
@Tag(name = "发票管理")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "recipient", description = "收件人", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "phone", description = "电话", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "address", description = "详细地址", in = ParameterIn.QUERY, ref = "String")
    })
    @RequiresPermissions("yckj:address:page")
    public Result<PageData<AddressDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<AddressDTO> page = addressService.page(params);

        return new Result<PageData<AddressDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("yckj:address:info")
    public Result<AddressDTO> get(@PathVariable("id") Long id) {
        AddressDTO data = addressService.get(id);

        return new Result<AddressDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("yckj:address:save")
    public Result save(@RequestBody AddressDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setCreateTime(new Date());
        dto.setUpdateTime(new Date());

        addressService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("yckj:address:update")
    public Result update(@RequestBody AddressDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        dto.setUpdateTime(new Date());

        addressService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("yckj:address:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        addressService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("yckj:address:export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AddressDTO> list = addressService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "发票管理", list, AddressExcel.class);
    }

}